<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem v-if="cateInfo.fcate"  :to="`/category/${cateInfo.fcate.id}`" >{{cateInfo.fcate.name}}</XtxBreadItem>
    <Transition  name='fade-right' mode='out-in'>
      <XtxBreadItem v-if="cateInfo.scate" :key="cateInfo.scate.id">{{cateInfo.scate.name}}</XtxBreadItem>
    </Transition>
  </XtxBread>
</template>
<script>
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed } from 'vue'
export default {
  name: 'SubBread',
  setup () {
    // 创建store实例
    const store = useStore()
    // 创建route实例
    const route = useRoute()
    // 计算属性 获取一级列表和二级列表的数据
    const cateInfo = computed(() => {
      const info = {
        fcate: null,
        scate: null
      }
      store.getters.cates.some(firstCate => {
        const secondCate = firstCate.children && firstCate.children.find(item => item.id === route.params.id)

        if (secondCate) {
          info.fcate = {
            id: firstCate.id,
            name: firstCate.name
          }
          info.scate = {
            id: secondCate.id,
            name: secondCate.name
          }

          // 终止后续遍历
          return true
        }
      })
      return info
    })
    // if (route.fullPath === '/category/sub' + route.params.id) {

    // }
    // console.dir(cateInfo)
    return { cateInfo }
  }
}
</script>
